package com.mobi.cacti.rabbit.controllers;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.mobi.cacti.rabbit.connectors.AbstractServerConnector;
import com.mobi.cacti.rabbit.connectors.ServerConnectorCallback;
import com.mobi.cacti.rabbit.connectors.TicketListServerConnector;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqTicketCount;
import com.mobi.cacti.rabbit.response.ReqTicketHistory;
import com.mobi.cacti.rabbit.response.ReqTicketRecent;
import com.mobi.cacti.rabbit.utils.SAXXMLParser;

public class TicketListActivityController implements ServerConnectorCallback{
	
	private TicketListServerConnector recentTicketListServerConnector;
	private TicketListServerConnector historyTicketListServerConnector;
	
	public TicketListActivityControllerCallback delegate;
	private static TicketListActivityController instance = null;

	public static TicketListActivityController getInstance() {
		if(instance == null) {
			instance = new TicketListActivityController();
		}
		return instance;
	}
	
	public void loadRecentTicketListForCurcuitId(Context context, String curcuitID)
	{
		if(!Cache.isNetworkAvailable(context))
		{
			Cache.showNoInternetAlertDialog(context);
			return ;
		}
		
		recentTicketListServerConnector = new TicketListServerConnector();
		recentTicketListServerConnector.delegate = this;
		recentTicketListServerConnector.loadTicketRecentListForCurcit(curcuitID);
	}
	
	public void loadHistoryTicketListForCurcuitId(Context context, String curcuitID)
	{
		if(!Cache.isNetworkAvailable(context))
		{
			Cache.showNoInternetAlertDialog(context);
			return ;
		}
		
		historyTicketListServerConnector = new TicketListServerConnector();
		historyTicketListServerConnector.delegate = this;
		historyTicketListServerConnector.loadTicketHistoryListForCurcit(curcuitID);
	}

	@Override
	public void serverConnectorFinishBackgroundTask(
			AbstractServerConnector serverConnector, Object response) {
		
		String requestCircuitID = ((String[]) response)[0];
		String responseData = ((String[]) response)[1];
		
		if(serverConnector == recentTicketListServerConnector)
		{
			Log.i("tai", "Recent Ticket list = " + requestCircuitID + " >>> " + responseData);

			InputStream is = new ByteArrayInputStream(responseData.getBytes());
			ArrayList<ReqTicketRecent> recentArrayList = SAXXMLParser.respTicketRecentList(is);

//			Log.i("tai", "Recent list = " + recentArrayList.get(0).ticketId + " < " + recentArrayList.get(0).readStatus);
			
			this.delegate.ticketListActivityControllerDidLoadRecentTicketList(recentArrayList);
		}
		else if(serverConnector == historyTicketListServerConnector)
		{
			Log.i("tai", "History Ticket list = " + requestCircuitID + " >>> " + responseData);
			
//			responseData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
//					"<ReqTFASTicketListHistory>" +
//					"<ReqSessionID>COMiQ201307290910310001</ReqSessionID>" +
//					"<ResSessionID>COMiSRTLH201310191522</ResSessionID>" +
//					"<Ticket_List>" +
//					"<Ticket_No id=\"T000034_1 \"/>" +
//					"</Ticket_List>" +
//					"</ReqTFASTicketListHistory>";
			
			InputStream is = new ByteArrayInputStream(responseData.getBytes());
			ArrayList<ReqTicketHistory> historyArrayList = SAXXMLParser.respTicketHistoryList(is);
			
			//Log.i("tai", "History list = " + historyArrayList.get(0).ticketId);
			
			this.delegate.ticketListActivityControllerDidLoadHistoryList(historyArrayList);
		}

		
	}

}
